﻿
$.fn.NoteItemEditor = function (options) {
    var opts = $.extend({}, $.fn.NoteItemEditor.defaults, options);
    return this.each(function () {
        var $this = $(this);
        $this.state = opts.state;

        $this.html('<input type="text" style="float:left"  data-gridMargin="0px,4px,0px,0px" value="@value" readonly="true" class="NoteItemEditor-readonly NoteItemEditor-editor" data-gridColumn="0" data-gridRow="0">'.replace('@value', $this.html().trim()) + "</input>" +
         '<span data-gridColumn="1" data-gridRow="0"><input type="button" class="button NoteItemEditor-button NoteItemEditor-btnEdit" value="edit" /><input type="button" class="button NoteItemEditor-button NoteItemEditor-btnSave" value="save" style="display:none" /><input type="button" class="button NoteItemEditor-button NoteItemEditor-btnDelete" value="delete" /></span>');

        var editor = $this.find('.NoteItemEditor-editor');
        var edit = $this.find('.NoteItemEditor-btnEdit');
        var save = $this.find('.NoteItemEditor-btnSave');
        var del = $this.find('.NoteItemEditor-btnDelete');
        $(edit).click(function () {
            gotoState("edit");
        });

        $(save).click(function () {
            JsUtil.toggleShowHide(true, opts.waitingItem);
            var note = { Index: $this.attr("data-Index"), FName: $this.attr("data-FName"), State: "Update", Text: $(editor).attr("value") };
            $.ajax({
                url: opts.postHref,
                type: "POST",
                data: JsUtil.AddAntiForgeryToken(JSON.stringify(note)),
                contentType: "application/json; charset=utf-8",
                fail: function () {
                    JsUtil.toggleShowHide(false, opts.waitingItem);
                    alert('FAIL');
                },
                error: function () {
                    JsUtil.toggleShowHide(false, opts.waitingItem);
                    alert('ERROR');
                },
                success: function () {
                    JsUtil.toggleShowHide(false, opts.waitingItem);
                    gotoState("view");
                }
            });
        });

        $(del).click(function () {
            JsUtil.toggleShowHide(true, opts.waitingItem);
            var note = { Index: $this.attr("data-Index"), FName: $this.attr("data-FName"), State: "Delete", Text: $(editor).attr("value") };
            $.ajax({
                url: opts.postHref,
                type: "POST",
                data: JsUtil.AddAntiForgeryToken(JSON.stringify(note)),
                contentType: "application/json; charset=utf-8",
                fail: function () {
                    JsUtil.toggleShowHide(false, opts.waitingItem);
                    alert('FAIL');
                },
                error: function () {
                    JsUtil.toggleShowHide(false, opts.waitingItem);
                    alert('ERROR');
                },
                success: function (evt) {
                    JsUtil.toggleShowHide(false, opts.waitingItem);
                    if (opts.ondelete != undefined) {
                        opts.ondelete(evt);
                    }
                }
            });
        });


        function gotoState(state) {
            if (state == "edit") {
                $(editor).removeClass('NoteItemEditor-readonly');
                $(editor).addClass('NoteItemEditor-edit');

                $(edit).css("display", "none");
                $(save).css("display", "");
                $(editor).removeAttr("readonly");
                $(editor).focus();
            }

            if (state == "view") {
                $(editor).removeClass('NoteItemEditor-edit');
                $(editor).addClass('NoteItemEditor-readonly');

                $(edit).css("display", "");
                $(save).css("display", "none");
                $(editor).attr("readonly", "true");
            }

        }
    });
}

$.fn.NoteItemEditor.defaults = {
    postHref: location.href,
    state: 'view',
    onsuccess: undefined,
    ondelete: undefined,
    waitingItem: undefined
}